<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Admin;
use App\Model\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Validator;

/**
 * 管理员
 */
class AdminController extends Controller
{

    // 用户信息
    public function info(Request $request)
    {

        $role = Role::find($request->_admin->role_id);
        $admin = [
            'role' => $role->role,
            'role_name' => $role->role_name,
            'name' => $request->_admin->name,
            'token' => $request->_admin->token,
            'create_time' => $request->_admin->create_time,
            'last_login_time' => $request->_admin->last_login_time,
        ];

        return resJson(self::HTTP_0_SUCCESS, $admin);
    }
    // 修改密码
    public function editPassword(Request $request)
    {
        $old_password = $request->old_password;
        $new_password = $request->new_password;
        $new_password2 = $request->new_password2;
        if ($new_password !== $new_password2) {
            return resJson(self::CUSTOM_10002_REPASSWORD_ERROR);
        }
        $id = $request->_admin->id;
        $admin = Admin::find($id);
        if ($admin) {
            if (encrypt_password($old_password, $admin->salt) === $admin->password) {
                $admin->salt = Str::random(10);
                $admin->password = encrypt_password($new_password, $admin->salt);
                $admin->save();
                return resJson(self::HTTP_0_SUCCESS);
            } else {
                return resJson(self::CUSTOM_10001_PASSWORD_ERROR);
            }
        } else {
            return resJson(self::HTTP_500_ERROR);
        }
    }
    // 登出
    public function logout(Request $request)
    {
        $id = $request->_admin->id;

        $admin = Admin::find($id);
        if ($admin) {
            $admin->token = '';
            $admin->save();
        }
        return resJson(self::HTTP_0_SUCCESS);
    }

    /******************************************************************************************************************* */
    // 列表
    public function index(Request $request)
    {
        $search = $request->search ?: '';
        $search = trim($search);
        $query = Admin::select('sf_admin.id', 'sf_admin.name', 'sf_admin.nickname', 'sf_admin.phone', 'sf_admin.role_id', 'sf_admin.status', 'sf_admin.remarks', 'sf_role.role_name');
        if ($search !== '') {
            $query->where(function ($query) use ($search) {
                return $query->Orwhere('name', 'like', "%{$search}%");
            });
        }
        $admin = $query->leftJoin('sf_role', 'sf_admin.role_id', '=', 'sf_role.id')
            ->where('status', 0) // 没有删除的
            ->orderBy('id', 'desc')
            ->get();
        return resJson(self::HTTP_0_SUCCESS, $admin);
    }

    public function create(Request $request)
    {
        $inputs = $request->only('name', 'nickname', 'phone', 'password', 'role_id');
        $validator = Validator::make($inputs, [
            'name' => ['required', 'unique:sf_admin', 'max:255'],
            'password' => ['required', 'min:6'],
        ], [
            'name.required' => '用户名称必填',
            'name.unique' => '用户名称不能重复',
            'password.reuired' => '密码必填',
            'password.min' => '密码长度不能<=6',
        ]);
        if ($validator->fails()) {
            $errors = $validator->errors();
            return resJson(self::HTTP_500_ERROR, $errors->first());
        }
        $salt = Str::random(10);
        $inputs['password'] = encrypt_password($inputs['password'], $salt);
        $inputs['salt'] = $salt;
        $inputs['created_time'] = millisecond();
        $admin = Admin::create($inputs);
        return resJson(self::HTTP_0_SUCCESS, $admin);
    }

    // 修改信息
    public function update(Request $request, $id)
    {
        $admin = Admin::find($id);
        $inputs = $request->only('role_id', 'nickname', 'phone');
        if ($admin) {
            $admin->fill($inputs);
            $admin->save();
            return resJson(self::HTTP_0_SUCCESS);
        } else {
            return resJson(self::HTTP_500_ERROR);
        }
    }

    // 重置密码123456
    public function resetPassword($id)
    {
        $admin = Admin::find($id);
        if ($admin) {
            $admin->salt = Str::random(10);
            $admin->password = encrypt_password(123456, $admin->salt);
            $admin->save();
            return resJson(self::HTTP_0_SUCCESS);
        } else {
            return resJson(self::HTTP_500_ERROR);
        }
    }

    public function destroy($id)
    {
        $admin = Admin::find($id);
        if ($admin) {
            // $admin->status = 1;
            $admin->delete();
            return resJson(self::HTTP_0_SUCCESS);
        } else {
            return resJson(self::HTTP_500_ERROR);
        }
    }
}
